package org.ngbx.springboot.demo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.ngbx.springboot.demo.infrastructure.constant.KafkaConstant;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class KafkaProducerController {
    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;

    @Resource
    private ObjectMapper objectMapper;

    // 发送消息
    @PostMapping("/kafka/message")
    public void sendMessage(@RequestBody KafkaMessage message) throws JsonProcessingException {
        kafkaTemplate.send(KafkaConstant.TEST_TOPIC, objectMapper.writeValueAsBytes(message));
    }
}
